﻿using KqAutoDesign.Models;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace KqAutoDesign.SonViews
{
    /// <summary>
    /// NewModule.xaml 的交互逻辑
    /// </summary>
    public partial class NewModule : Window, INotifyPropertyChanged
    {
        public NewModule()
        {
            InitializeComponent();
            Module = new ModuleBase();
            DataContext = this;
        }

        public NewModule(int id):this()
        {
            _id = id;
            Module.Id = id;
        }

        int _id = 0;
        public ObservableCollection<RegistedApp> registedApps
        { 
            get
            {
                return App.GlobalConfigs.Apps;
            } 
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private ModuleBase _module;
        public ModuleBase Module 
        {
            get {  return _module; }
            set
            {
                if (_module != value)
                {
                    _module = value;
                    RaisePropertyChanged(nameof(Module));
                }
            }
        }

        private void SelBatchScriptAction(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "批处理脚本|*.bat";
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                Module.BatchScript = new Script();
                Module.BatchScript.OrgFilePath = openFileDialog.FileName;
                Module.BatchScript.Id = 1;
                Module.BatchScript.Sctp = ScriptTypeEnum.BatchScript;
                Guid newGuid = Guid.NewGuid();
                Module.BatchScript.FileName = newGuid.ToString()+System.IO.Path.GetExtension(openFileDialog.FileName);
                Module.BatchScript.Name = "运行脚本";
                batchFileTxt.Text = openFileDialog.FileName;
            }
        }

        private void SelFuncScriptsAction(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "功能脚本|*.*";
            openFileDialog.Multiselect = true;
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                funcFilesTxt.Text = string.Empty;
                Module.FunctionScripts = new ObservableCollection<Script>();
                for (int i = 0; i < openFileDialog.FileNames.Length; i++)
                {
                    Script script = new Script();
                    script.OrgFilePath = openFileDialog.FileNames[i];
                    script.Id = i+1;
                    script.Sctp = ScriptTypeEnum.FunctionScript;
                    Guid newGuid = Guid.NewGuid();
                    script.FileName = newGuid.ToString() + System.IO.Path.GetExtension(script.OrgFilePath);
                    script.Name = "功能脚本"+(i+1).ToString();
                    Module.FunctionScripts.Add(script);
                    funcFilesTxt.Text += openFileDialog.FileNames[i];
                    if (i < openFileDialog.FileNames.Length - 1)
                        funcFilesTxt.Text += ";";
                }
            }
        }

        private void OkBtnAction(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(Module.Name))
            {
                MessageBox.Show("模块名称不能为空!", "提示", MessageBoxButton.OK);
                return;
            }
            if (Module.App == null)
            {
                MessageBox.Show("App不能为空!", "提示", MessageBoxButton.OK);
                return;
            }
            if (Module.BatchScript == null )
            {
                MessageBox.Show("运行脚本不能为空!", "提示", MessageBoxButton.OK);
                return;
            }
            if (Module.FunctionScripts==null||Module.FunctionScripts.Count <= 0)
            {
                MessageBox.Show("功能脚本不能为空!", "提示", MessageBoxButton.OK);
                return;
            }
            DialogResult = true;
            Close();
        }

        private void CancelBtnAction(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}
